package org.kaindorf.kainflix.metadata.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter< ?>> converters) {
        converters.add(new StringHttpMessageConverter()); // for text/plain
        converters.add(createXmlHttpMessageConverter()); // for application/xml or text/xml
        
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setPrettyPrint(true);
        converters.add(mappingJackson2HttpMessageConverter); // for application/json
        
        super.configureMessageConverters(converters);
    }

    private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
        MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();

        XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
        xmlConverter.setMarshaller(xstreamMarshaller);
        xmlConverter.setUnmarshaller(xstreamMarshaller);
        
        xstreamMarshaller.setAutodetectAnnotations(true);

        return xmlConverter;
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
        configurer.favorPathExtension(true);
        configurer.mediaType("xml", MediaType.APPLICATION_XML);
    }
    
    /**
     * Configure resource handlers for loading static resources (css,
     * javascript, ...). Also able to configure a folder with static html files
     * or something like that.
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/res/**").addResourceLocations("classpath:/res/") //
                .setCachePeriod(3600); // one hour
    }
    
//    @Bean
//    public TemplateResolver thymeleafTemplateResolver() {
//        
//        // maybe choose SpringResourceTemplateResolver?
//        TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
//        templateResolver.setPrefix("templates/");
//        templateResolver.setSuffix(".html");
//        
//        // non-caching enables hot-deploying views (great for development)
//        templateResolver.setCacheable(false);
//
//        // legacy html is not as strict as XHTML and makes writing HTML easier
//        // (you don't have to close every tag like input or link)
//        // Had to import net.sourceforge.nekohtml::nekohtml::1.9.15 for legacy html
//        templateResolver.setTemplateMode("LEGACYHTML5");
//
//        return templateResolver;
//    }
//
//    /**
//     * Configures how Thymeleaf should process the templates.
//     *
//     * @param thymeleafTemplateResolver bean injected by spring (configured
//     * above)
//     * @return
//     */
//    @Bean
//    public SpringTemplateEngine thymeleafTemplateEngine(TemplateResolver thymeleafTemplateResolver) {
//        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//        templateEngine.setTemplateResolver(thymeleafTemplateResolver);
//
//        // the spring standard dialect is configured automatically
//        
//        return templateEngine;
//    }
//
//    /**
//     * Configures how views should be resolved. Thymeleaf will only resolve it
//     * when they match a specific pattern.
//     *
//     * @param thymeleafTemplateEngine
//     * @return
//     */
//    @Bean
//    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine thymeleafTemplateEngine) {
//        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
//        viewResolver.setTemplateEngine(thymeleafTemplateEngine);
//        viewResolver.setViewNames(new String[]{"*.html", "*.xhtml"});
//        return viewResolver;
//    }

//    @Bean
//    public InternalResourceViewResolver internalJspViewResolver() {
//        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//        viewResolver.setViewClass(JstlView.class); // optional? but goes for jstl directly?
//        viewResolver.setPrefix("views/");
//        // viewResolver.setSuffix(".jsp");
//        viewResolver.setOrder(1);
////        viewResolver.setViewNames(new String[] { "*.jsp" });
//        return viewResolver;
//    }

}
